<template>
  <div class="main">
    <el-container>
      <el-aside :width="collapseFold ? '60px' : '250px'">
        <main-menu :collapseFold="collapseFold" />
      </el-aside>
      <el-container>
        <el-header>
          <main-header @fold-change="foldChange" />
        </el-header>
        <el-main>
          <el-config-provider :locale="zhCn">
            <router-view v-slot="{ Component }">
              <transition enter-active-class="animate__animated  animate__fadeIn">
                <component v-if="showComponent" :is="Component"></component>
              </transition>
            </router-view>
          </el-config-provider>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import MainMenu from '@/components/main-menu/main-menu.vue'
import MainHeader from '@/components/main-header/main-header.vue'
import { TOKEN } from '@/global/storage'
import { localCache } from '@/utils/Cache'
import { useRouter } from 'vue-router'
import { ref, nextTick, watch } from 'vue'
import mittBus from '@/utils/mittBus'
const router = useRouter()
const collapseFold = ref(false)
const showComponent = ref(true)
const refreshPage = ref(false)
// 退出登录的操作
const loginExitClick = () => {
  // 清除token返回登录页
  localCache.removeCache(TOKEN)
  router.push('/login')
}
// 接收子组件传过来的状态
function foldChange(isFold: boolean) {
  collapseFold.value = isFold
}
mittBus.on('refreshPage', () => {
  refreshPage.value = !refreshPage.value
})
watch(
  () => refreshPage.value,
  () => {
    showComponent.value = false
    nextTick(() => {
      showComponent.value = true
    })
  }
)
defineExpose({
  loginExitClick
})
</script>

<style lang="less" scoped>
.main {
  height: 100%;
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: var(--el--side--bg--);
    transition: width 0.3s linear;
    overflow-x: hidden;
    overflow-y: auto;
    &::-webkit-scrollbar {
      display: none;
    }
  }
  .el-main {
    background-color: var(--el--dark--bg--);
  }
}
</style>
